﻿using Inaction.Core;
using Inaction.Rule;
using System;
using System.IO;

namespace Inaction.Extensions.Serialization
{
    public class XmlSerializer : BaseSerializer
    {
        public override string SerializeToString(object obj)
        {
            return System.Text.Encoding.Unicode.GetString(Serialize(obj));
        }
        public override byte[] Serialize(object obj)
        {
            var serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
            using (MemoryStream ms = new MemoryStream())
            {
                serializer.Serialize(ms, obj);
                return ms.ToArray();
            }
        }
        public override T Deserialize<T>(byte[] data)
        {
            return (T)Deserialize(typeof(T), data);
        }
        public override object Deserialize(Type type, byte[] data)
        {
            var serializer = new System.Xml.Serialization.XmlSerializer(type);
            MemoryStream ms = new MemoryStream(data);
            return serializer.Deserialize(ms);
        }

        public override SerializeType SerializeType
        {
            get { return SerializeType.Xml; }
        }
    }
}
